package com.qf.medicaldoctoradmin.healthMonitor.drugInfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qf.common.entity.DrugFavorite;
import com.qf.common.entity.DrugInfo;
import com.qf.common.utils.RedisUtils;
import com.qf.common.utils.UserUtils;
import com.qf.medicaldoctoradmin.healthMonitor.drugInfo.dao.DrugFavoriteDao;
import com.qf.medicaldoctoradmin.healthMonitor.drugInfo.dao.DrugInfoDao;
import com.qf.medicaldoctoradmin.healthMonitor.drugInfo.service.DrugFavoriteService;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DrugFavoriteServiceImpl implements DrugFavoriteService {
    @Resource
    private DrugFavoriteDao drugFavoriteDao;

    @Resource
    private DrugInfoDao drugInfoDao;

    @Override
    public void favoriteInfo(Integer drugId) {
        if (drugId == null) {
            throw new RuntimeException("请先选择收藏的文章id");
        }
        Integer uid = UserUtils.getUid();
        String favoriteKey = String.format("drug:favorite:%s", drugId);
        String todayFavoriteNumKey = String.format("drug:favoriteNum:%s", drugId);

        Integer hasKey = (Integer) RedisUtils.hashGet(favoriteKey, uid.toString());
        if (hasKey == null || hasKey.equals(0)) {
            // 将收藏记录存到redis
            RedisUtils.hashPut(favoriteKey, uid.toString(), 1);

            // 如果有数据且为0，代表取消收藏，再次收藏
            if (hasKey != null) {
                drugFavoriteDao.update(null,
                        new LambdaUpdateWrapper<DrugFavorite>()
                                .set(DrugFavorite::getFavoriteStatus, 1)
                                .eq(DrugFavorite::getUserId, uid)
                                .eq(DrugFavorite::getDrugId, drugId));
            } else {
                DrugFavorite drugFavorite = new DrugFavorite();
                drugFavorite.setDrugId(drugId);
                drugFavorite.setUserId(uid);
                drugFavorite.setFavoriteStatus(1);
                // 添加到数据库
                drugFavoriteDao.insert(drugFavorite);
            }

            // 收藏数加1
            RedisUtils.hashIncr(todayFavoriteNumKey, drugId.toString(), 1);
        } else {

            // 取消收藏, redis设为0
            RedisUtils.hashPut(favoriteKey, uid.toString(), 0);

            // 数据库收藏状态设为0
            drugFavoriteDao.update(null,
                    new LambdaUpdateWrapper<DrugFavorite>()
                            .set(DrugFavorite::getFavoriteStatus, 0)
                            .eq(DrugFavorite::getUserId, uid)
                            .eq(DrugFavorite::getDrugId, drugId));

            // 收藏数减1
            RedisUtils.hashIncr(todayFavoriteNumKey, uid.toString(), -1);
        }

    }

    @Override
    public List<DrugInfo> getFavoriteList() {
        List<DrugFavorite> drugFavorites = drugFavoriteDao.selectList(
                new LambdaQueryWrapper<DrugFavorite>()
                        .eq(DrugFavorite::getFavoriteStatus, 1)
        );

        List<Integer> drugIds = drugFavorites.stream()
                .map(DrugFavorite::getDrugId)
                .collect(Collectors.toList());

        List<DrugInfo> drugInfos = new ArrayList<>();
        if (!drugIds.isEmpty()) {
            drugInfos = drugInfoDao.selectList(new LambdaQueryWrapper<DrugInfo>()
                    .in(DrugInfo::getId, drugIds)); // 使用in条件进行查询
        }

        return drugInfos;
    }
}

